﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.AcmsControls.ContentControls.FrontEnd;
using Acms.Bll;

namespace Ait.Web.Templates.Parent
{
    public partial class _default : Acms.AcmsControls.TemplatePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsChildren_SelectData);
            dsCaseStudies.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsCaseStudies_SelectData);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (acmsDownload1.IsEmpty)
                liDownload1.Visible = false;
            if (acmsDownload2.IsEmpty)
                liDownload2.Visible = false;
            if (acmsDownload3.IsEmpty)
                liDownload3.Visible = false;
            if (acmsDownload1.IsEmpty && acmsDownload2.IsEmpty && acmsDownload3.IsEmpty)
            {
                phDownloads.Visible = false;
            }
        }

        void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
        {
            e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion));
        }

        protected void dsCaseStudies_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
        {
            var pathParts = AcmsContext.CurrentPost.Path.Split('/');
            //if (pathParts.Length > 1)
            //    form1.Attributes["class"] = "section-" + pathParts[1];
                var parentPath="~/"+pathParts[1];
            
            var sectionPost= Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, parentPath,  AcmsContext.SiteVersion);
            
            var caseStudies = new List<Post>();
            if (AcmsContext.CurrentPost != null)
            {
                GetCaseStudies(sectionPost, caseStudies);
            }

            e.SetData(caseStudies);
        }

        private void GetCaseStudies(IEnumerable<Post> posts, List<Post> caseStudies)
        {
            if (posts == null)
                return;

            foreach (var post in posts)
            {
                if (post.TemplateName == "CaseStudiesItem")
                {
                    caseStudies.Add(post);
                    phCaseStudies.Visible = true;
                }
            }
            foreach (var post in posts)
            {
                GetCaseStudies(post.Children, caseStudies);
            }
        }

        protected void acmsShowCTAImage_DataBound(object sender, EventArgs e)
        {
            phShowCTAImage.Visible = acmsShowCTAImage.Value;
        }

    }
}
